package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.TopicInfo;

/**
 * 推荐Adapter
 * 
 * @author zhuchen
 * 
 */
public class RecommendHeadAdapter extends BaseAdapter {

    private Context context;
    private int itemWidth;
    private int itemHeigth;
    private List<TopicInfo> list;

    public RecommendHeadAdapter(Context context, List<TopicInfo> list, int itemWidth, int itemHeigth) {
        this.list = list;
        this.context = context;
        this.itemHeigth = itemHeigth;
        this.itemWidth = itemWidth;
    }

    @Override
    public int getCount() {
        return (list == null || list.size() == 0) ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public TopicInfo getItem(int position) {
        return list == null ? null : list.get(position % list.size());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Container con = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.recommend_iv_cover, null);
            con = new Container();
            con.img = (ImageView) convertView.findViewById(R.id.main_iv_cover);
            convertView.setTag(con);
        } else {
            con = (Container) convertView.getTag();
        }

        if (list == null || list.size() == 0 || getItem(position) == null) {
            return convertView;
        }

        final Container con2 = con;

        android.view.ViewGroup.LayoutParams params = con2.img.getLayoutParams();
        if (params != null) {
            params.height = itemHeigth;
            params.width = itemWidth;
            con2.img.setLayoutParams(params);
        }

        SqImageManager.getImageManager().displayImage(getItem(position).getImgUrl(), con2.img,
                SqImageManager.IMG_DEFAULT,R.drawable.icon_def_bookimg_banner);
        return convertView;
    }

    static class Container {
        ImageView img;
    }

    public void setList(List<TopicInfo> list) {
        this.list = list;

    }

}
